package com.zhiwei.tool;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat dateInFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000'Z'");
    public static StartAndEnd getDates(String start,String end){
        StartAndEnd startAndEnd = new StartAndEnd();
        if (isDate(start)){
            try {
                startAndEnd.setStart(dateFormat.format(dateInFormat.parse(start)));
            } catch (ParseException e) {}
        }else {
            try {
                startAndEnd.setStart("1994-01-01 00:00:00");
            } catch (Exception e) { }
        }
        if (isDate(end)){
            try {
                startAndEnd.setEnd(dateFormat.format(dateInFormat.parse(end)));
            } catch (ParseException e) {}
        }else {
            startAndEnd.setEnd(dateFormat.format(new Date()));
        }
        return startAndEnd;
    }

    public static boolean isDate(String date){
        try {
            if (StringUtils.hasLength(date)){
                dateInFormat.parse(date);
                return true;
            }else {
                return false;
            }
        }catch (ParseException | IllegalArgumentException e){
//            new Exception("startType or endType is not a Date type! it's String type don't mention" ).printStackTrace();
            return false;
        }
    }
}
